package com.iplant.base.utils.general;

import java.util.HashMap;
import java.util.Map;

import javax.validation.constraints.NotNull;

public class LockHelper {

	private static Map<String, LockHelper> LockHelperMap = new HashMap<>();

	public synchronized static LockHelper getLock(@NotNull String wLockName) {

		if (!LockHelperMap.containsKey(wLockName)) {
			synchronized (LockHelper.class) {
				if (!LockHelperMap.containsKey(wLockName)) {
					LockHelperMap.put(wLockName, new LockHelper());
				}
			}
		}

		LockHelperMap.get(wLockName).LockName = wLockName;

		return LockHelperMap.get(wLockName);
	}

	public synchronized static LockHelper getLock(@NotNull Class<?> wClazz) {
 
		String wLockName = wClazz.getName();
		return LockHelper.getLock(wLockName);
	}
	public synchronized static LockHelper getLock(@NotNull Class<?> wClazz,String wLockName) {

		wLockName = wClazz.getName()+"_"+wLockName;
		return LockHelper.getLock(wLockName);
	}

	private LockHelper() {

		Count++;
	}

	public String LockName = "";

	public static int Count = 0;
}
